package com.pri.chat.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.IBinder;
import android.text.Html;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.netease.nim.uikit.common.util.string.StringUtil;
import com.netease.nim.uikit.net.SharedHelper;
import com.netease.nim.uikit.net.StatusBarUtil;
import com.netease.nim.uikit.net.entity.BaseBean;
import com.netease.nim.uikit.net.entity.MessageBean;
import com.netease.nim.uikit.net.entity.WishListBean;
import com.netease.nim.uikit.net.entity.WishRecordBean;
import com.netease.nim.uikit.net.entity.WishXinLuBean;
import com.netease.nim.uikit.net.rxjava.HttpMethods;
import com.netease.nim.uikit.net.subscribers.ProgressSubscriber;
import com.netease.nim.uikit.net.subscribers.SubscriberOnNextListener;
import com.pri.chat.MainActivity;
import com.pri.chat.R;
import com.pri.chat.adapter.DTAdapter;
import com.pri.chat.adapter.DialogCommentAdapter;
import com.pri.chat.adapter.WishAllRecordAdapter;
import com.pri.chat.adapter.WishXinLuAdapter;
import com.pri.chat.dialog.DialogUtil;
import com.pri.chat.fragment.WishMsgFragment;
import com.pri.chat.model.EvaListModel;
import com.pri.chat.model.MessageModel;
import com.pri.chat.model.WishModel;
import com.pri.chat.model.ZanModel;
import com.pri.chat.utils.Des3Util;
import com.pri.chat.utils.GlideUtil;
import com.pri.chat.utils.OnItemLongClickListener;
import com.pri.chat.utils.StringUtils;
import com.pri.chat.utils.TimeUtils;
import com.pri.chat.view.ImageViewPlus;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.vondear.rxui.view.dialog.RxDialogSureCancel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.SocketHandler;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 描述：添加类的描述
 *
 * @author
 * @time 2020/4/28
 */
public class WishMsgActivity extends AppCompatActivity {
    @BindView(R.id.backBtn)
    ImageView backBtn;
    @BindView(R.id.iv_logo)
    ImageViewPlus ivLogo;
    @BindView(R.id.tv_dt_name)
    TextView tvDtName;
    @BindView(R.id.sex_bg)
    View sex_bg;
    @BindView(R.id.sex_logo)
    ImageView sex_logo;
    @BindView(R.id.tv_dt_age)
    TextView tvDtAge;

    @BindView(R.id.vip_bg)
    TextView vip_bg;
    @BindView(R.id.vip_super_bg)
    TextView vip_super_bg;

    @BindView(R.id.iv_share)
    View iv_share;
    @BindView(R.id.tv_wish_finish)
    ImageView tv_wish_finish;

    @BindView(R.id.tv_wish_desc)
    TextView tvWishDesc;
    @BindView(R.id.circle_layout)
    ImageView circleLayout;
    @BindView(R.id.tv_wish_go)
    TextView tvWishGo;
    @BindView(R.id.tv_wish_time)
    TextView tvWishTime;
    @BindView(R.id.rec_record)
    RecyclerView recRecord;
    @BindView(R.id.rec_record1)
    RecyclerView recRecord1;
    @BindView(R.id.iv_none)
    ImageView ivNone;
    @BindView(R.id.srl_wish)
    SmartRefreshLayout srlWish;
    @BindView(R.id.tv_zl_num)
    TextView tvZlNum;

    @BindView(R.id.xinluLinear)
    View xinluLinear;

    @BindView(R.id.rec_record_pl)
    RecyclerView rec_record_pl;


    @BindView(R.id.zanLinear)
    View zanLinear;
    @BindView(R.id.zanLogo)
    ImageView zanLogo;
    @BindView(R.id.zanNum)
    TextView zanNum;

    @BindView(R.id.plLinear)
    View plLinear;
    @BindView(R.id.plNum)
    TextView plNum;

    @BindView(R.id.ll_huifu)
    View ll_huifu;
    @BindView(R.id.et_input)
    EditText et_input;
    @BindView(R.id.tv_send_message)
    TextView tv_send_message;

    //期限
    @BindView(R.id.tv_wish_qixian)
    TextView tv_wish_qixian;

    @BindView(R.id.tabLinear1)
    View tabLinear1;
    @BindView(R.id.tvXllc)
    TextView tvXllc;
    @BindView(R.id.xllcLine)
    View xllcLine;
    //    @BindView(R.id.tv_zl_num)
//    TextView tv_zl_num;
    @BindView(R.id.zljlLine)
    View zljlLine;

    @BindView(R.id.tabLinear2)
    View tabLinear2;

    @BindView(R.id.addXinLu)
    View addXinLu;
    @BindView(R.id.zheCeng)
    View zheCeng;




    private String id;
    private WishListBean wishListBean;
    private int position;
    private WishAllRecordAdapter mAdapter;
    private List<WishRecordBean> mList;
    private int page = 1;
    private boolean isShowDialog = true;

    private WishXinLuAdapter mAdapter1;
    private List<WishXinLuBean> mList1;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wish_msg_layout);
        View view = findViewById(R.id.titleRelate);
        StatusBarUtil.setColor(this, getResources().getColor(R.color.white), 0);
        StatusBarUtil.setTranslucentForImageViewInFragment(this, 0, view);
        StatusBarUtil.setLightMode(this);
        ButterKnife.bind(this);

        wishListBean = getIntent().getExtras().getParcelable("wish");
        if (wishListBean == null) {
            id = getIntent().getExtras().getString("id");
            getWishDetail();
        } else {
            position = getIntent().getExtras().getInt("position");
            initMsg();
            initRec();
            initRec1();
            initRecPl();
            initSrl();
            initData();
            getPingLun();
        }
    }


    private void initSrl() {
        srlWish.setEnableLoadmore(false);
        srlWish.setOnRefreshListener(refresh_layout -> {
            refresh_layout.finishRefresh(1000);
            refresh();
        });
//        srlWish.setOnLoadmoreListener(refresh_layout -> {
//            refresh_layout.finishLoadmore(1000);
//            isShowDialog = false;
//            if (type == 0) {
//                page++;
//                initData();
//            } else if (type == 2) {
//                plPage++;
//                getPingLun();
//            }
//        });
    }

    private void refresh() {
        page = 1;
        isShowDialog = false;
        mList1.clear();
        mAdapter1.notifyDataSetChanged();
        initData1();
    }

    private void initMsg() {
        GlideUtil.getInstance().loadHeadImg(this, ivLogo, wishListBean.getHeadPic());
        insertData();
    }

    private void insertData() {
        String memberId = SharedHelper.readId(this);

        if (wishListBean.getMemberId().equals(memberId)) {
            //本人
            addXinLu.setVisibility(View.VISIBLE);
            tabLinear1.setVisibility(View.VISIBLE);
            tabLinear2.setVisibility(View.GONE);
        } else {
            //别人
            addXinLu.setVisibility(View.GONE);
            tabLinear1.setVisibility(View.GONE);
            tabLinear2.setVisibility(View.VISIBLE);
        }
        srlWish.setEnableLoadmore(false);

        if (wishListBean.getStatus() == 1) {
            tvWishGo.setVisibility(View.GONE);
            tv_wish_finish.setVisibility(View.VISIBLE);
        } else {
            if (memberId.equals(wishListBean.getMemberId())) {
                tvWishGo.setText("完成愿望");
                tvWishGo.setVisibility(View.VISIBLE);
                tvWishGo.setBackgroundResource(R.drawable.btn_orange_shape);
            } else {
                if (wishListBean.getCanHelp() == 0) {
                    tvWishGo.setText(" 帮Ta完成 ");
                    tvWishGo.setVisibility(View.VISIBLE);
                    tvWishGo.setBackgroundResource(R.drawable.btn_orange_shape);
                } else {
                    tvWishGo.setText(" 帮Ta完成 ");
                    tvWishGo.setVisibility(View.VISIBLE);
                    tvWishGo.setBackgroundResource(R.drawable.btn_gray_shape);
                }
            }
            tv_wish_finish.setVisibility(View.GONE);
        }

        if(StringUtils.isEmpty(wishListBean.getQixian())){
            tv_wish_qixian.setText("愿望期限：无期限");
        }else{
            if(wishListBean.getQixian().contains("-")){
                long now = System.currentTimeMillis();
                long rq = TimeUtils.getTime(wishListBean.getQixian()+" 23:59:59");
                long day = (rq - now) / (1000L*3600L*24L);
                if(day < 0l){
                    tv_wish_qixian.setText("愿望期限：已过期");
                }else{
                    tv_wish_qixian.setText("愿望期限：剩余"+day+"天");
                }
            }else{
                tv_wish_qixian.setText("愿望期限："+wishListBean.getQixian());
            }

        }


        tvDtAge.setText(wishListBean.getAge() + "");
        plNum.setText(wishListBean.getPlNum() + "");
        zanNum.setText(wishListBean.getZanNum() + "");
        if (wishListBean.getSfdz() == 1) {
            zanLogo.setImageResource(R.mipmap.ic_zan_selected);
        } else {
            zanLogo.setImageResource(R.mipmap.ic_zan_gray_normal);
        }
        tvDtName.setText(wishListBean.getMemberName());
        tvWishDesc.setText(Html.fromHtml((StringUtils.isEmpty(wishListBean.getBiaoqian()) ?"":"<font color='#A396F5'>"+wishListBean.getBiaoqian()+"</font> ")+wishListBean.getContent()));
        String cInfo = "";
        if (!StringUtils.isEmpty(wishListBean.getCity())) {
            cInfo = "·" + wishListBean.getCity();
        }
        tvWishTime.setText(TimeUtils.computeFromNowStr(wishListBean.getAddDate()) + cInfo);
        GlideUtil.getInstance().loadHeadImg(this, ivLogo, wishListBean.getHeadPic());
//        Glide.with(this)
//                .load(wishListBean.getPicList())
//                .centerCrop()
//                .into(circleLayout);

        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.img_default)//图片加载出来前，显示的图片
                .fallback(R.mipmap.img_default) //url为空的时候,显示的图片
                .error(R.mipmap.img_default)
                .transform(new CenterCrop(), new RoundedCorners(20));

        Glide.with(this).load(wishListBean.getPicList()).apply(options).into(circleLayout);

        //        circleLayout.setImageUrls(wishListBean.getImgs());
//        circleLayout.setSpacing(10f);
//        circleLayout.setOnItemClickListener((view, position) -> {
//            PictureSelector.create(this)
//                    .themeStyle(R.style.picture_default_style)
//                    .isNotPreviewDownload(true)
//                    .loadImageEngine(GlideEngine.createGlideEngine()) // 请参考Demo GlideEngine.java
//                    .openExternalPreview(position, wishListBean.getMediaList());
//        });


        if (wishListBean.getSex().equals("男")) {
            sex_logo.setImageResource(R.mipmap.man);
            sex_bg.setBackgroundResource(R.drawable.btn_sex_man);
        } else {
            sex_logo.setImageResource(R.mipmap.woman);
            sex_bg.setBackgroundResource(R.drawable.btn_sex_woman);
        }
        if ("是".equals(wishListBean.getMemberLevel())) {
            vip_bg.setText("VIP");
            vip_bg.setVisibility(View.VISIBLE);
        } else {
            vip_bg.setVisibility(View.GONE);
        }

        if ("是".equals(wishListBean.getMemberLevel())) {
            vip_super_bg.setText("SVIP");
            vip_super_bg.setVisibility(View.VISIBLE);
        } else {
            vip_super_bg.setVisibility(View.GONE);
        }
    }

    private void initRec() {
        mList = new ArrayList<>();
        LinearLayoutManager linear = new LinearLayoutManager(this);
//        recRecord.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        recRecord.setLayoutManager(linear);
        recRecord.setNestedScrollingEnabled(false);
        mAdapter = new WishAllRecordAdapter(R.layout.item_wish_record_all, mList, wishListBean.getMemberId());
        recRecord.setAdapter(mAdapter);
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_status:
//                        httpAgreeOrder(position, 0);
                        break;
                    case R.id.tv_cancel:
                        break;
                    case R.id.iv_logo:
                        Bundle bundle = new Bundle();
                        bundle.putString("id", mList.get(position).getMemberId());
                        RxActivityTool.skipActivity(WishMsgActivity.this, OtherPeopleMsgActivity.class, bundle);
                        break;
                    case R.id.btn_tongyi:
                        WishRecordBean mrb = mList.get(position);
                        if (mrb.getStatus() == 0) {
                            httpAgreeOrder(position, 1);
                        } else if (mrb.getStatus() == 1) {
                            httpAgreeOrder(position, 2);
                        } else if (mrb.getStatus() == 2) {
                            httpAgreeOrder(position, 21);
                        }
                        break;
                    case R.id.btn_jujue:
                        httpAgreeOrder(position, 3);
                        break;
                }
            }


        });

    }


    private void initRec1() {
        mList1 = new ArrayList<>();
        LinearLayoutManager linear = new LinearLayoutManager(this);
        recRecord1.setLayoutManager(linear);
        recRecord1.setNestedScrollingEnabled(false);
        mAdapter1 = new WishXinLuAdapter(R.layout.item_wish_xinlu_all, mList1);
        recRecord1.setAdapter(mAdapter1);
        mAdapter1.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                initDialog("确定删除该数据？", position, 0);
                return false;
            }
        });
        mAdapter1.setOnImageClickListener(new WishXinLuAdapter.OnImageClickListener() {
            @Override
            public void onItemClick(View view, List<String> imgList, int c_pos) {
//                PictureSelector.create(mContext)
//                        .themeStyle(R.style.picture_default_style)
//                        .isNotPreviewDownload(true)
//                        .loadImageEngine(GlideEngine.createGlideEngine()) // 请参考Demo GlideEngine.java
//                        .openExternalPreview(c_pos, mList.get(f_pos).getMediaList());
                Bundle bundle = new Bundle();
                bundle.putInt("item", c_pos);
                bundle.putSerializable("listPath", (Serializable) imgList);
                RxActivityTool.skipActivity(WishMsgActivity.this, NetPicShowActivity.class, bundle);

            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        mList1.clear();
        initData1();
    }

    private void initRecPl() {
        mCommentList = new ArrayList<>();
        LinearLayoutManager linear = new LinearLayoutManager(this);
        rec_record_pl.setLayoutManager(linear);
        rec_record_pl.setNestedScrollingEnabled(false);
        mCommentAdapter = new DialogCommentAdapter(R.layout.item_message, mCommentList);
        rec_record_pl.setAdapter(mCommentAdapter);
        mCommentAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                initDialog("确定删除该数据？", position, 1);
                return false;
            }
        });
    }


    private void httpAgreeOrder(int position, int status) {
        SubscriberOnNextListener<BaseBean> onNextListener = baseBean -> {
            RxToast.normal(baseBean.getMsg());
            if (!baseBean.isSuccess()) {
                return;
            }
            if (status == 1) {
                wishListBean.setStatus(1);
                wishListBean.setCanHelp(1);
            } else if (status == 2) {
                wishListBean.setStatus(2);
                wishListBean.setCanHelp(1);
            } else if (status == 3) {
                wishListBean.setStatus(0);
                wishListBean.setCanHelp(0);
            }
            insertData();
            WishRecordBean mrb = mList.get(position);
            mrb.setStatus(status);
            mAdapter.setData(position, mrb);

        };
        WishModel dtModel = new WishModel(status);
        dtModel.setId(mList.get(position).getId());
        dtModel.setWishId(wishListBean.getId());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(dtModel));
        HttpMethods.getInstance().updateWishHelp(new ProgressSubscriber<>(onNextListener, this, isShowDialog), requestBody);

    }

    protected void initData() {
        SubscriberOnNextListener<BaseBean<List<WishRecordBean>>> onNextListener = baseBean -> {
            tvZlNum.setText("助力记录(" + baseBean.getData().size() + ")");
            if (baseBean.getData().isEmpty() && page == 1) {
                mAdapter.notifyDataSetChanged();

            } else {
                mList.addAll(baseBean.getData());
                mAdapter.notifyDataSetChanged();
            }
        };

        EvaListModel evaListModel = new EvaListModel();
        evaListModel.setWishId(wishListBean.getId());
        evaListModel.setObjId(SharedHelper.readId(this));
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(evaListModel));
        HttpMethods.getInstance().listWishHelp(new ProgressSubscriber<>(onNextListener, this, isShowDialog), requestBody);
    }

    private int plPage = 1;
    private List<MessageBean> mCommentList = new ArrayList<>();
    private DialogCommentAdapter mCommentAdapter;

    protected void getPingLun() {
        mCommentList.clear();
        SubscriberOnNextListener<BaseBean<List<MessageBean>>> onNextListener = baseBean -> {
            if (baseBean.getData().isEmpty() && page == 1) {
                mCommentAdapter.notifyDataSetChanged();
            } else {
                mCommentList.addAll(baseBean.getData());
                mCommentAdapter.notifyDataSetChanged();
            }
        };
        EvaListModel evaListModel = new EvaListModel();
        evaListModel.setPageIndex(plPage);
        evaListModel.setObjId(wishListBean.getId());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(evaListModel));
        HttpMethods.getInstance().listPingLun(new ProgressSubscriber<>(onNextListener, this, isShowDialog), requestBody);
    }

    protected void initData1() {
        SubscriberOnNextListener<BaseBean<List<WishXinLuBean>>> onNextListener = baseBean -> {
            if (baseBean.getData().isEmpty() && page == 1) {
                ivNone.setVisibility(View.VISIBLE);
                mAdapter1.notifyDataSetChanged();
            } else {
                ivNone.setVisibility(View.GONE);
                mList1.addAll(baseBean.getData());
                mAdapter1.notifyDataSetChanged();
            }
        };

        EvaListModel evaListModel = new EvaListModel();
        evaListModel.setWishId(wishListBean.getId());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(evaListModel));
        HttpMethods.getInstance().listWishXinLu(new ProgressSubscriber<>(onNextListener, this, isShowDialog), requestBody);
    }


    protected void delWishXinLu(int position) {
        SubscriberOnNextListener<BaseBean> onNextListener = baseBean -> {
            tvZlNum.setText("心路历程(" + (mList1.size() - 1) + ")");
            mAdapter1.remove(position);
        };

        Map<String, Object> params = new HashMap<>();
        params.put("id", mList1.get(position).getId());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(params));
        HttpMethods.getInstance().delWishXinLu(new ProgressSubscriber<>(onNextListener, this, isShowDialog), requestBody);
    }

    /**
     * 删除评论
     */
    private void delPingLun(int pos) {
        SubscriberOnNextListener<BaseBean<List<BaseBean>>> onNextListener = baseBean -> {
            RxToast.normal(baseBean.getMsg());
            if (baseBean.isSuccess()) {
                mCommentList.remove(pos);
                mCommentAdapter.notifyItemRemoved(pos);
                wishListBean.setPlNum(wishListBean.getPlNum() - 1);
                plNum.setText(wishListBean.getPlNum() + "");
                plNum.setText("最新评论(" + wishListBean.getPlNum() + ")");
//                tv_pinglun_num.setText("评论记录（" + wishListBean.getPlNum() + "）");
            }
        };

        Map<String, Object> params = new HashMap<>();
        params.put("pingLunId", mCommentList.get(pos).getId());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(params));
        HttpMethods.getInstance().delPingLun(new ProgressSubscriber<>(onNextListener, this, isShowDialog), requestBody);
    }

    protected void getWishDetail() {
        SubscriberOnNextListener<BaseBean<WishListBean>> onNextListener = baseBean -> {
            if (baseBean.isSuccess()) {
                wishListBean = baseBean.getData();
                List<String> list = new ArrayList<>();
                list.add(wishListBean.getPicList());
                wishListBean.setImgs(list);
                initMsg();
                initRec();
                initSrl();
                initData();
                getPingLun();
            }
        };

        Map<String, Object> params = new HashMap<>();
        params.put("memberId", SharedHelper.readId(WishMsgActivity.this));
        params.put("otherMemberId", id);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(params));
        HttpMethods.getInstance().getWishById(new ProgressSubscriber<>(onNextListener, this, isShowDialog), requestBody);
    }

    private void httpGoWish(int status) {
        SubscriberOnNextListener<BaseBean> onNextListener = baseBean -> {
            RxToast.normal("操作成功");
            tvWishGo.setBackgroundResource(R.drawable.btn_gray_20_shape);
            wishListBean.setStatus(1);
            wishListBean.setCanHelp(1);
            insertData();
            refresh();

            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            intent.setAction(MainActivity.DELETE_WISH);
            bundle.putParcelable("wishObj", wishListBean);
            intent.putExtras(bundle);
            sendBroadcast(intent);
        };
        WishModel dtModel = new WishModel(status);
        dtModel.setMemberId(SharedHelper.readId(this));
        dtModel.setWishId(wishListBean.getId());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(dtModel));
        HttpMethods.getInstance().saveWishHelp(new ProgressSubscriber<>(onNextListener, this, isShowDialog), requestBody);
    }

    private void finishWish() {
        SubscriberOnNextListener<BaseBean> onNextListener = baseBean -> {
            RxToast.normal("操作成功");
            wishListBean.setStatus(1);
            wishListBean.setCanHelp(1);
            insertData();
            refresh();

            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            intent.setAction(MainActivity.UPDATE_WISH);
            bundle.putParcelable("wishObj", wishListBean);
            intent.putExtras(bundle);
            sendBroadcast(intent);
        };
        Map<String, Object> params = new HashMap<>();
        params.put("id", wishListBean.getId());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(params));
        HttpMethods.getInstance().finishWish(new ProgressSubscriber<>(onNextListener, this, isShowDialog), requestBody);
    }

    private void httpLike() {
        SubscriberOnNextListener<BaseBean> onNextListener = baseBean -> {
            if (wishListBean.getSfdz() == 0) {
                wishListBean.setSfdz(1);
                wishListBean.setZanNum(wishListBean.getZanNum() + 1);
            } else {
                wishListBean.setSfdz(0);
                wishListBean.setZanNum(wishListBean.getZanNum() - 1);
            }
            zanNum.setText(wishListBean.getZanNum() + "");
            if (wishListBean.getSfdz() == 1) {
                zanLogo.setImageResource(R.mipmap.ic_zan_selected);
            } else {
                zanLogo.setImageResource(R.mipmap.ic_zan_normal);
            }
            RxToast.normal(baseBean.getMsg());
        };
        ZanModel zanModel = new ZanModel();
        zanModel.setBdzId(wishListBean.getId());
        zanModel.setDzrId(SharedHelper.readId(this));
        zanModel.setType(3);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(zanModel));
        HttpMethods.getInstance().saveDianZan(new ProgressSubscriber<>(onNextListener, this, true), requestBody);
    }

    // 隐藏软键盘
    private void HideSoftInput(IBinder token) {
        if (token != null) {
            InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            manager.hideSoftInputFromWindow(token,
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    private void httpSendMessage() {
        HideSoftInput(ll_huifu.getWindowToken());
        SubscriberOnNextListener<BaseBean<List<MessageBean>>> onNextListener = baseBean -> {
            RxToast.normal("评论成功");
            et_input.setText("");
            wishListBean.setPlNum((wishListBean.getPlNum() + 1));
            plNum.setText(wishListBean.getPlNum() + "");
//            ll_huifu.setVisibility(View.GONE);
            getPingLun();
        };
        Gson gson = new Gson();
        MessageModel circleEnity = new MessageModel();
        circleEnity.setType(2);
        circleEnity.setObjId(wishListBean.getId());
        circleEnity.setMemberId(SharedHelper.readId(this));
        circleEnity.setContent(et_input.getText().toString());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(circleEnity));
        HttpMethods.getInstance().savePingLun(new ProgressSubscriber<>(onNextListener, this, false), requestBody);
    }

    @OnClick({R.id.zheCeng,R.id.tv_send_message, R.id.plLinear, R.id.zanLinear, R.id.addXinLu, R.id.tv_zl_num, R.id.tv_xllc_linear, R.id.backBtn, R.id.iv_logo, R.id.tv_wish_go, R.id.circle_layout, R.id.iv_share})
    public void onViewClicked(View view) {
        Bundle bundle = new Bundle();
        switch (view.getId()) {
            case R.id.tv_send_message:
                if (StringUtils.isEmpty(et_input.getText().toString())) {
                    RxToast.normal("请输入内容");
                    return;
                }
                httpSendMessage();
                break;
            case R.id.plLinear:
                ll_huifu.setVisibility(View.VISIBLE);
                zheCeng.setVisibility(View.VISIBLE);
                break;
            case R.id.zheCeng:
                ll_huifu.setVisibility(View.GONE);
                zheCeng.setVisibility(View.GONE);
                break;
            case R.id.zanLinear:
                httpLike();
                break;
            case R.id.addXinLu:
                //发布心路
                Bundle bundle1 = new Bundle();
                bundle1.putString("wishId", wishListBean.getId());
                RxActivityTool.skipActivity(this, ReleaseWishXinLuAcctivity.class, bundle1);
                break;
            case R.id.tv_zl_num:
                tvZlNum.setTextSize(18);
                tvZlNum.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
                zljlLine.setVisibility(View.VISIBLE);

                tvXllc.setTextSize(14);
                tvXllc.setTypeface(Typeface.DEFAULT);
                xinluLinear.setVisibility(View.GONE);
                xllcLine.setVisibility(View.INVISIBLE);
                recRecord.setVisibility(View.VISIBLE);
                srlWish.setEnableRefresh(true);
                break;
            case R.id.tv_xllc_linear:
                tvXllc.setTextSize(18);
                tvXllc.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
                tvZlNum.setTextSize(14);
                tvZlNum.setTypeface(Typeface.DEFAULT);
                xinluLinear.setVisibility(View.VISIBLE);
                xllcLine.setVisibility(View.VISIBLE);
                zljlLine.setVisibility(View.INVISIBLE);
                recRecord.setVisibility(View.GONE);
                srlWish.setEnableRefresh(false);
                break;
            case R.id.iv_logo:
                bundle.putString("id", wishListBean.getMemberId());
                RxActivityTool.skipActivity(this, OtherPeopleMsgActivity.class, bundle);
                break;
            case R.id.backBtn:
                Intent intent = new Intent();
                intent.putExtra("position", position);
                intent.putExtra("bean", wishListBean);
                setResult(RESULT_OK, intent);
                finish();
                break;
            case R.id.tv_wish_go:
                if ("完成愿望".equals(tvWishGo.getText().toString())) {
                    finishWish();
                } else {
                    if (wishListBean.getCanHelp() == 0) {
                        httpGoWish(0);
                    }
                }
                break;
            case R.id.circle_layout:
//                PictureChoiceUtil.getInstance().choiceTouPic(this, wishListBean.getMediaList());
//                Intent intents= new Intent(this,PicBigActivity.class);
//                intents.putExtra("path",wishListBean.getPicList());
//                startActivity(intents);


                bundle.putInt("item", 0);
                bundle.putSerializable("listPath", (Serializable) wishListBean.getImgs());
                RxActivityTool.skipActivity(this, NetPicShowActivity.class, bundle);

                break;
            case R.id.iv_share:
                //分享
                initShareDialog();
                break;
        }
    }

    private Dialog mShareDialog;

    private void initShareDialog() {
        String memberId = SharedHelper.readId(this);
        String listMemberId = wishListBean.getMemberId();
        int type = 1;
        if (listMemberId.equals(memberId)) {
            type = 2;
        }
        mShareDialog = DialogUtil.getInstance().showShareDialog(this, type);
        TextView title = mShareDialog.findViewById(R.id.title);
        title.setText("让好友帮忙实现");
        DialogUtil.getInstance().setOnShareClickListener(new DialogUtil.OnShareClickListener() {
            @Override
            public void qqOnClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("id", wishListBean.getId());
                bundle.putString("image", wishListBean.getImgs().get(0));
                bundle.putString("title", wishListBean.getMemberName());
                bundle.putString("content", wishListBean.getContent());
                bundle.putInt("type", 1);
                RxActivityTool.skipActivity(WishMsgActivity.this, ShareMemberActivity.class, bundle);
                mShareDialog.dismiss();
            }

            @Override
            public void circleOnClick(View v) {
                mShareDialog.dismiss();
                DialogUtil.getInstance().shareWeChat(wishListBean.getPicList(), HttpMethods.SHARE_TITLE, "我发布了" + wishListBean.getContent() + "，快啦帮我实现！", HttpMethods.SHARE_URL);
            }

            @Override
            public void qZoneOnClick(View v) {
                mShareDialog.dismiss();
                //删除操作
                deleteWish();
            }

            @Override
            public void weixinOnClick(View v) {
                mShareDialog.dismiss();
                DialogUtil.getInstance().shareWeChat(wishListBean.getPicList(), HttpMethods.SHARE_TITLE, "我发布了" + wishListBean.getContent() + "，快啦帮我实现！", HttpMethods.SHARE_URL);
            }

            @Override
            public void jubaoOnClick(View v) {
                mShareDialog.dismiss();
                Bundle bundle = new Bundle();
                bundle.putString("id",wishListBean.getId());
                bundle.putString("type","2");
                RxActivityTool.skipActivity(WishMsgActivity.this, ReleaseJuBaoAcctivity.class,bundle);

            }
        });
        mShareDialog.show();
    }

    private void initDialog(String content, int pos, int type1) {
        RxDialogSureCancel rxDialogEditSureCancel = new RxDialogSureCancel(this);
        rxDialogEditSureCancel.getLogoView().setVisibility(View.GONE);
        rxDialogEditSureCancel.getTitleView().setTextSize(18);
        rxDialogEditSureCancel.getTitleView().setTextColor(Color.BLACK);
        rxDialogEditSureCancel.setTitle("提示");
        rxDialogEditSureCancel.setContent(content);
        rxDialogEditSureCancel.setCancelListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rxDialogEditSureCancel.dismiss();
            }
        });
        rxDialogEditSureCancel.setSureListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rxDialogEditSureCancel.dismiss();
                if (type1 == 0) {
                    delWishXinLu(pos);
                } else {
                    delPingLun(pos);
                }
            }
        });
        rxDialogEditSureCancel.show();
    }


    private void deleteWish() {
        SubscriberOnNextListener<BaseBean> onNextListener = baseBean -> {
            RxToast.normal("删除成功！");

            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            intent.setAction(MainActivity.DELETE_WISH);
            bundle.putParcelable("wishObj", wishListBean);
            intent.putExtras(bundle);
            sendBroadcast(intent);

            finish();
        };
        Map<String, Object> params = new HashMap<>();
        params.put("id", wishListBean.getId());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(params));
        HttpMethods.getInstance().deleteWish(new ProgressSubscriber<>(onNextListener, WishMsgActivity.this, isShowDialog), requestBody);
    }
}

